home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / CheckBoxWithLabel / CheckBoxWithLabel.cs next >
Encoding:
Text File  |  2001-01-15  |  1.7 KB  |  54 lines

  1. //------------------------------------------------
  2. // CheckBoxWithLabel.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CheckBoxWithLabel: Form
  9. {
  10.      Label label;
  11.  
  12.      public static void Main()
  13.      {
  14.           Application.Run(new CheckBoxWithLabel());
  15.      }
  16.      public CheckBoxWithLabel()
  17.      {
  18.           Text = "CheckBox Demo with Label";
  19.  
  20.           int      cyText   = Font.Height;
  21.           int      cxText   = cyText / 2;
  22.           string[] astrText = {"Bold", "Italic", "Underline", "Strikeout"};
  23.  
  24.           label = new Label();
  25.           label.Parent   = this;
  26.           label.Text     = Text + ": Sample Text";
  27.           label.AutoSize = true;
  28.  
  29.           for (int i = 0; i < 4; i++)
  30.           {
  31.                CheckBox chkbox = new CheckBox();
  32.                chkbox.Parent = this;
  33.                chkbox.Text = astrText[i];
  34.                chkbox.Location = new Point(2 * cxText, 
  35.                                                (4 + 3 * i) * cyText / 2);
  36.                chkbox.Size = new Size(12 * cxText, cyText);
  37.                chkbox.CheckedChanged += 
  38.                               new EventHandler(CheckBoxOnCheckedChanged);
  39.           }
  40.      }
  41.      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
  42.      {
  43.           FontStyle   fs   = 0;
  44.           FontStyle[] afs  = { FontStyle.Bold,      FontStyle.Italic, 
  45.                                FontStyle.Underline, FontStyle.Strikeout };
  46.  
  47.           for (int i = 0; i < 4; i++)
  48.                if (((CheckBox) Controls[i + 1]).Checked)
  49.                     fs |= afs[i];
  50.  
  51.           label.Font = new Font(label.Font, fs);
  52.      }
  53. }
  54.